
import myRequest from './request.js'
import { getSignIn } from '@/api/personalCenter/index.js'
import { salesmanGetSignIn } from '@/api/invoicer/index.js'

let _flag = true
let subsq = []
export const addreq = (r) => {
  subsq.push(r)
}

export const setRefreshToken = async () => {
  let token = uni.getStorageSync("token")
  let standing = uni.getStorageSync("standing")
  let username = uni.getStorageSync("username")
  let password = uni.getStorageSync("password")
  let res = ""
  if (_flag) {
    _flag = false
    if (token) {
      if (standing == '供应商') {
        //当前是客户登录
        res = await salesmanGetSignIn({ username: username, password: password })
      } else {
        //当前是业务员登录
        res = await getSignIn({ username: username, password: password })
      }
      if (res.code === 200) {
        uni.setStorageSync('token', res.data.accessToken)
        subsq.forEach(r => {
          myRequest(r.url, r.method, r.data, r.header)
        })
        subsq = []
        _flag = true
      } else {
        // 退出登录
        uni.showToast({ icon: 'none', title: '登录过期，请重新登录' })
        uni.navigateTo({ url: '/pages_user/reg/index' })
      }
    }
  }
}


